<?php

namespace App\Http\Middleware;

use App\Lib\Code;
use App\Services\ApiResponseService;
use Closure;
use Illuminate\Support\Facades\Auth;

class CheckAuthType
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     * @param string                    $role
     *
     * @return mixed
     */
    public function handle($request, Closure $next, $role='proxy')
    {
        $user = Auth::user();
        if ($role == 'proxy') {
            if ($user->profile->role == 1) {
                return $next($request);
            }
        } else if ($role == 'bus'){
            if ($user->profile->role == 2) {
                return $next($request);
            }
        }
        return ApiResponseService::showError(Code::NOTAUTH, '无权访问，请检查您的账户权限');
    }
}
